<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-row>
        <el-col>
          <el-form-item label="数据库" prop="dbId">
            <el-select v-model="queryParams.dbId" placeholder="请选择数据库">
              <el-option v-for="item in arrDb" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col>
          <el-form-item label="语句" prop="name">
            <el-input
              type="textarea"
              v-model="queryParams.name"
              placeholder="请输入语句"
              clearable
              style="width: 800px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">执行</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <hr>
    <el-row :gutter="10" class="mb8" v-show="data.length>0">
      <el-col :span="1.5">
        <el-table v-loading="loading" :data="data">
          <el-table-column v-for="(item, index) in dataTitle" :key="index" :label="item">
            <template scope="scope">
              <span>{{ scope.row[item] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {doExecute} from "@/api/base/task";
import {listDb} from "@/api/base/db";

export default {
  name: "Extcute",
  data() {
    return {
      loading: false,
      arrDb: [],
      queryParams: {
        dbId: null,
        name: ""
      },
      dataTitle: [],
      data: [],
    };
  },
  created() {
    listDb().then(res => {
      this.arrDb = res.rows.map(d => {
        return {value: d.dbId, label: d.name}
      });
    })
  },
  methods: {
    handleQuery() {
      doExecute(this.queryParams).then(res => {
        let d = JSON.parse(res.msg);
        if (d.length > 0) {
          this.dataTitle = Object.keys(d[0]);
        }
        this.data = d;
      })
    },
  }
};
</script>
